{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Generate dataset from a plain data folder. \n",
    "\n",
    "\n",
    "Some finance news articles are stored in `sample_finance_data` folder. In this tutorial, we are going to generate `InstructionDataset` from a data folder (finance news articles) and perform instruction fine-tuning on the generated dataset.\n",
    "\n",
    "Use token from OpenAI to generate dataset for fine-tuning.\n",
    "You can get token from [here](https://beta.openai.com/docs/api-reference/authentication).\n",
    "You can also use AI Studio and Cohere to generate dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "from xturing.datasets import InstructionDataset\n",
    "from xturing.model_apis.openai import ChatGPT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "engine = ChatGPT(\"your-api-key\")\n",
    "dataset = InstructionDataset.generate_dataset_from_dir(engine=engine, path=\"./sample_finance_data\")\n",
    "\n",
    "# Alternatively, you can use Cohere to generate dataset\n",
    "\n",
    "# from xturing.model_apis.cohere import Medium\n",
    "#\n",
    "# engine = Medium(\"your-api-key\")\n",
    "\n",
    "# Alternatively, you can use AI21 to generate dataset\n",
    "\n",
    "# from xturing.model_apis.ai21 import J2Grande\n",
    "#\n",
    "# engine = J2Grande(\"your-api-key\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "716ad054108d4ffb9407a8ba424b4f31",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Saving the dataset (0/1 shards):   0%|          | 0/80 [00:00<?, ? examples/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "dataset.save(\"./output_dataset\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from xturing.models import BaseModel\n",
    "model = BaseModel.create(\"gpt2_lora\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Finetune the model on generated dataset\n",
    "model.finetune(dataset=dataset)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
